package org.gvp.gateway.controller;

import lombok.RequiredArgsConstructor;
import org.gvp.common.annotation.Operation;
import org.gvp.common.http.Result;
import org.gvp.gateway.dto.MysqlStatus;
import org.gvp.gateway.service.MySqlInfoService;
import org.gvp.gateway.service.RootService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

@RestController
@RequestMapping("/root")
@RequiredArgsConstructor
public class RootController {
    private final RootService rootService;
    private final MySqlInfoService mySqlInfoService;

    @Operation( value = "刷新用户登录后显示的菜单信息", param = "用户名")
    @GetMapping("/refresh/user-menu/{username}")
    public Mono<Result<Long>> refreshUserMenu(@PathVariable String username){
        return this.rootService.flushUserMenu(username)
                .map(Result::ok);
    }

    @Operation(value = "刷新菜单树,系统可用的全部菜单树")
    @GetMapping("/refresh/menu-tree")
    public Mono<Result<Long>> refreshMenuTree(){
        return this.rootService.flushMenuTree().map(Result::ok);
    }

    @Operation(value = "刷新用户登录信息")
    @GetMapping("/refresh/login-user/{username}")
    public Mono<Result<Long>> refreshLoginUser(@PathVariable String username){
        return this.rootService.flushLoginUser(username).map(Result::ok);
    }

    @GetMapping("/refresh/role-path/{roleName}")
    @Operation(value = "刷新角色路径权限关系")
    public Mono<Result<Long>> refreshRolePath(@PathVariable String roleName){
        return this.rootService.flushRolePath(roleName).map(Result::ok);
    }


    @Operation("检查网关服务器是否可以连接")
    @GetMapping("/status/connect")
    public Mono<Result<Boolean>> testConnect(){
        return this.rootService.check();
    }

    @Operation("获取网关的mysql数据库主从复制状态")
    @GetMapping("/status/mysql")
    public Mono<Result<MysqlStatus>> queryMysqlInfo(){
        return this.mySqlInfoService.getMysqlInfo();
    }

    @Operation("添加用户的时候,获取加密密码")
    @GetMapping("/pwd/encryption/{password}")
    public Mono<Result<String>> encryption(@PathVariable String password){
       return Mono.just(Result.<String>ok(this.rootService.encryption(password)));
    }
}
